clear all
*cap log close
set more off
set seed 1 


/*******************************************************************************
- Plot MTE from various specifications
*******************************************************************************/

***************************
*** first, redo non param
use "${data}/out/4-main", clear
gen Y = cite_ny1 
gen D = harsh 
gen W = covbin1
keep citationid Y D Z W officerid totfe

** calculate propensity score
reghdfe D Z, absorb(totfe, savefe) res
predict pscore1, xbd

** require pscore btw [0, 1]
replace pscore1 = 1 if pscore1>1 & ~mi(pscore1)
replace pscore1 = 0 if pscore1<0

cap rm temp.dta
sort pscore1
binsreg Y pscore1, absorb(totfe) nbins(12) savedata(temp)

use temp, clear
gen dx = dots_x - dots_x[_n-1]
gen dy = dots_fit - dots_fit[_n-1]
gen dydx = dy/dx 

gen mid = dots_x[_n-1] + (dots_x - dots_x[_n-1])/2

keep dydx mid

tempfile nonparam
save 	`nonparam'

// gen x       = mid
// gen mtenp_x = dydx 
***************************

** read in data
use "${est}/mte_bootstrap.dta", replace

keep if it == 1

append using `nonparam'

** create figure
# delimit ;
twoway
(line mte_u_1 u if ~mi(u), lpattern(solid) lcolor(blue))
(line mte_u_2 u if ~mi(u), lpattern(dash) lcolor(red))
(line mte_u_3 u if ~mi(u), lpattern(dot) lcolor(green))
(line mte_u_4 u if ~mi(u), lpattern(dash_dot) lcolor(purple))
(line mte_u_5 u if ~mi(u), lpattern(longdash_dot) lcolor(black))
(scatter dydx mid if ~mi(mid), msymbol(diamond) color(orange) mfcolor(none) mlwidth(medthick))
,
legend(
label(1 "Linear")
label(2 "Quadratic")
label(3 "Cubic")
label(4 "Quartic")
label(5 "Quintic")
label(6 "Non-parametric")
pos(2) ring(0) rows(2) region(lstyle(border))
)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin))
yscale(range(-0.15, 0.15)) ylabel(-0.15(0.05)0.15)
yline(0,lpattern(dash) lcolor(gray) lwidth(thin))
ylab(,nogrid) xlab(,nogrid)
xtitle("Resistance to Treatment")
ytitle("Marginal Treatment Effect")
;
# delimit cr
graph export "${out}/apx_extrap/mte_figure.pdf", replace
rm temp.dta 
// ------------------------------------------------------------------------------


// mte table --------------------------------------------------------------------
** read in data
use "${est}/mte_bootstrap.dta", clear

** split the main data off
preserve
keep if it == 1
tempfile main
save `main'
restore

keep if it~=1

** sort data
bysort it (u): gen n = _n
keep if n == 1

** for each polynomial specification, get standard errors for ATE/ATT/ATU
forval k = 1/5{
	
	sum   ate_`k'
	local ate_`k'_se : di %6.3f `r(sd)'
	sum   att_`k'
	local att_`k'_se : di %6.3f `r(sd)'
	sum   atu_`k'
	local atu_`k'_se : di %6.3f `r(sd)'

	gen   diff_`k' = att_`k' - atu_`k'
	sum   diff_`k'
	local diff_`k'_se : di %6.3f `r(sd)'

}

** non-parametric mte
sum   ate_np
local ate_np_se : di %6.3f `r(sd)'
sum   att_np
local att_np_se : di %6.3f `r(sd)'
sum   atu_np
local atu_np_se : di %6.3f `r(sd)'

gen   diff_np = att_np - atu_np
sum   diff_np
local diff_np_se : di %6.3f `r(sd)'


use `main', clear

** 
forval k = 1/5{

	gen diff_`k' = att_`k' - atu_`k'

	local ATT`k'  : di %6.3f att_`k'[1]
	local ATU`k'  : di %6.3f atu_`k'[1]
	local ATE`k'  : di %6.3f ate_`k'[1]
	local Diff`k' : di %6.3f diff_`k'[1]

}

	gen diff_np = att_np - atu_np

	local ATTnp  : di %6.3f att_np[1]
	local ATUnp  : di %6.3f atu_np[1]
	local ATEnp  : di %6.3f ate_np[1]
	local Diffnp : di %6.3f diff_np[1]

//////////////////
// CREATE TABLE //
//////////////////

// "    & Average Treatment Effect & Average Treatment Effect & Average Treatment Effect &            \\" _n ///
// "    & (ATE)                    & on the Treated (ATT)     & on the Treated (ATU)     & T-TU Diff  \\" _n ///

* try looping through variables
file close _all
capture macro drop fh
file open fh using "${out}/apx_extrap/table_mte.tex", write replace

file write fh ///
"\begin{tabular}{l*{4}{c}}"  _n

file write fh ///
"\toprule" _n ///
"    & Average &  Treated & Untreated &            \\" _n ///
"\textit{MTE Specification}    & (ATE)     &  (ATT)     & (ATU)       & Diff  \\" _n ///
"\cmidrule(l){1-1}" _n ///
"\cmidrule(l){2-2}" _n ///
"\cmidrule(l){3-3}" _n ///
"\cmidrule(l){4-4}" _n ///
"\cmidrule(l){5-5}" _n

file write fh ///
"Linear     &  `ATE1'       &  `ATT1'      &  `ATU1'      & `Diff1'      \\" _n ///
"               &  (`ate_1_se') & (`att_1_se') & (`atu_1_se') & (`diff_1_se') \\" _n ///
"Quadratic  &  `ATE2'       &  `ATT2'      &  `ATU2'      & `Diff2'      \\" _n ///
"               &  (`ate_2_se') & (`att_2_se') & (`atu_2_se') & (`diff_2_se') \\" _n ///
"Cubic      &  `ATE3'       &  `ATT3'      &  `ATU3'      & `Diff3'      \\" _n ///
"               &  (`ate_3_se') & (`att_3_se') & (`atu_3_se') & (`diff_3_se') \\" _n ///
"Quartic    &  `ATE4'       &  `ATT4'      &  `ATU4'      & `Diff4'      \\" _n ///
"               &  (`ate_4_se') & (`att_4_se') & (`atu_4_se') & (`diff_4_se') \\" _n ///
"Quintic    &  `ATE5'       &  `ATT5'      &  `ATU5'      & `Diff5'      \\" _n ///
"               &  (`ate_5_se') & (`att_5_se') & (`atu_5_se') & (`diff_5_se') \\" _n ///
"Non-Parametric \, \, \, \, &  `ATEnp'       &  `ATTnp'      &  `ATUnp'      & `Diffnp'      \\" _n ///
"               &  (`ate_np_se') & (`att_np_se') & (`atu_np_se') & (`diff_np_se') \\" _n ///
"\bottomrule" _n ///
"\end{tabular}" _n
file close fh
// ------------------------------------------------------------------------------




